//
//  CalendarDayView.m
//  SCXCalendar
//
//  Created by 桑赐相 on 2017/12/13.
//  Copyright © 2017年 桑赐相. All rights reserved.
//

#import "CalendarDayView.h"
#import "CalendarDayCell.h"
#import "YXDateHelpObject.h"
#import "YXDateHelpObject.h"
@interface CalendarDayView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) CalendarDayModel *selectedModel;
@end

@implementation CalendarDayView


-(instancetype)initWithFrame:(CGRect)frame withDate:(NSDate *)date{
    self = [super initWithFrame:frame];
    if (self) {
        self.currentDate = date;
        [self initSubView];
    }
    return self;
}

-(void)initSubView{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshDay:) name:@"didSelectDay" object:nil];
    self.dataSource = [[YXDateHelpObject manager]getCurrentDate:self.currentDate];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout: [self layout]];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"CalendarDayCell" bundle:nil] forCellWithReuseIdentifier:@"CalendarDayCell"];
    [self addSubview:self.collectionView];
}

-(void)refreshDay:(NSNotification *)notification{
    CalendarDayModel *dayModel = notification.userInfo[@"dayModel"];
    self.selectedModel = dayModel;
    if (dayModel.isCurrentDate) {
        for (CalendarDayModel *model in self.dataSource) {
            model.isSelected = NO;
            if ([model.dateString isEqualToString:dayModel.dateString]) {
                model.isSelected = YES;
            }
        }
        [self.collectionView reloadData];
    }
}
-(void)setCurrentDate:(NSDate *)currentDate{
    _currentDate = currentDate;
    self.dataSource = [[YXDateHelpObject manager]getCurrentDate:self.currentDate];
    for (CalendarDayModel *model in self.dataSource) {
        model.isSelected = NO;
        if ([model.dateString isEqualToString:self.selectedModel.dateString]) {
            model.isSelected = YES;
        }
    }
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CalendarDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CalendarDayCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CalendarDayModel *model = [self.dataSource[indexPath.item] mutableCopy];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"didSelectDay" object:nil userInfo:@{@"dayModel":model}];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


-(UICollectionViewFlowLayout *)layout{
    CGFloat itemW = self.frame.size.width / 7;
    UICollectionViewFlowLayout *ly = [UICollectionViewFlowLayout new];
    ly.itemSize = CGSizeMake(itemW,itemW);
    ly.scrollDirection = UICollectionViewScrollDirectionVertical;
    ly.minimumLineSpacing = 0;
    ly.minimumInteritemSpacing = 0;
    ly.sectionInset = UIEdgeInsetsZero;
    return ly;
}

@end
